package com.sniper.springmvc.action.admin;

import com.sniper.springmvc.model.Collect;
import com.sniper.springmvc.mybatis.service.impl.CollectService;
import com.sniper.springmvc.utils.SystemConfigUtil;
import com.sniper.springmvc.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${adminPath}需要在当前当前类赋值
 *
 * @author suzhen
 */
@RequestMapping(value = "${adminPath}")
@Controller
public class AdminController extends AdminBaseController {


    /**
     * @return
     * @throws
     */
    @RequiresPermissions("admin:index:index")
    @RequestMapping("")
    public String index(Map<String, Object> map) {


        // 调用一些常用的数据

        return forward("/admin/admin/index.ftl");
    }

    /**
     * 获取首页顶部信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("topMenuValue")
    public Map<String, Object> topMemuValue() {


        return null;
    }

    /**
     * 处理用户每次访问的url
     *
     * @param title
     * @param url
     * @return
     */
    @ResponseBody
    @RequestMapping("collect")
    public List<Collect> collect(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "url", required = false) String url) {
        List<Collect> collects;
        if (ValidateUtil.isValid(title) && ValidateUtil.isValid(title)) {
            //去除空格
            title = title.replace("\\s", "");
            //判断是否包含竖线
            if (title.contains("|")) {
                title = title.substring(0, title.indexOf("|"));
            }
            collectService.insertValue(url, title, CollectService.URL_MENU,
                    getAdminUser());
        }

        int historyCollectNum = SystemConfigUtil.getInt("historyCollectNum");
        if (historyCollectNum <= 0) {
            historyCollectNum = 20;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", historyCollectNum);
        params.put("uid", getAdminUser().getId());
        params.put("order", "num desc");
        collects = collectService.query("select", params);
        return collects;
    }

}